package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * com.herench.bs.transfer.transfer
 * Created by wangwenkai on 2017/9/29/15:04
 * E-mail：605064334@qq.com
 * 查询预约挂号详情
 */
public class QueryAppointDetailsTransfer extends AbstractTransfer {

    public QueryAppointDetailsTransfer(String method, Client client,String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String appointId = (String) params.get("appointId");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        String idCard = (String) params.get("idCard");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRowElement.addAttribute("AppointCheckNo", appointId).addAttribute("PERSON_NO", "").addAttribute("PATIENT_ID", "");
        dataRowElement.addAttribute("START_DATE", startDate).addAttribute("END_DATE", endDate).addAttribute("appointWay ", "05").addAttribute("idNo", idCard);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new LinkedHashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if ("0".equals(executeStatus)) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            QueryAppointDetailsInnerMessage queryAppointDetailsInnerMessage = new QueryAppointDetailsInnerMessage(dataRow);
            resMap.put("deptName", queryAppointDetailsInnerMessage.getDeptName());
            resMap.put("deptId", queryAppointDetailsInnerMessage.getDeptCode());
            resMap.put("ampm", Dialect.getAmpmCode(queryAppointDetailsInnerMessage.getTimeDesc()));
            resMap.put("docName", queryAppointDetailsInnerMessage.getDoctor());
            resMap.put("docId", queryAppointDetailsInnerMessage.getDoctorCode());
            resMap.put("idCard", queryAppointDetailsInnerMessage.getIdNo());
            resMap.put("fee", queryAppointDetailsInnerMessage.getRegistFee());
            resMap.put("orderStatus", Dialect.getOrderStatusCode(queryAppointDetailsInnerMessage.getAppointStatus()));
            resMap.put("registerType", "2");
            resMap.put("appointDate", queryAppointDetailsInnerMessage.getAppointDate());
            resMap.put("categorCode", Dialect.getClinicTypeCode(queryAppointDetailsInnerMessage.getClinicType()));
            resMap.put("categorName", queryAppointDetailsInnerMessage.getClinicType());
            resMap.put("visitDate", queryAppointDetailsInnerMessage.getClinicVisitDate());
            resMap.put("appointId", queryAppointDetailsInnerMessage.getApptedId());
            resMap.put("visitTime", queryAppointDetailsInnerMessage.getHourStart() + "~" + queryAppointDetailsInnerMessage.getHourEnd());
            resMap.put("createTime", queryAppointDetailsInnerMessage.getAppointDate());
            resMap.put("name", queryAppointDetailsInnerMessage.getPatientName());
            resMap.put("medicalCard", queryAppointDetailsInnerMessage.getInsuranceNo());
            //1:医保卡
            resMap.put("medicalCardType", "1");
            resMap.put("chargeType", queryAppointDetailsInnerMessage.getChargeType());
            resMap.put("code", executeStatus);
            resMap.put("message", "查询预约详情成功！");
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }


    private class QueryAppointDetailsInnerMessage implements InnerMessage {

        /**
         * 预约日期
         */
        private String clinicVisitDate;
        /**
         * 号别名称
         */
        private String clinicLabel;
        /**
         * 时段
         */
        private String timeDesc;
        /**
         * 时段开始
         */
        private String hourStart;
        /**
         * 时段截止
         */
        private String hourEnd;
        /**
         * 预约序号
         */
        private String registOrder;
        /**
         * 预约识别码
         */
        private String apptedId;
        /**
         * 患者姓名
         */
        private String patientName;
        /**
         * 性别
         */
        private String sex;
        /**
         * 费别
         */
        private String chargeType;
        /**
         * 出生日期
         */
        private String dateOfBirth;
        /**
         * 社保卡号
         */
        private String insuranceNo;
        /**
         * 身份证号
         */
        private String idNo;
        /**
         * 联系电话
         */
        private String phoneNumber;
        /**
         * 预约发生时间
         */
        private String appointDate;
        /**
         * 建议就诊时间
         */
        private String appointScheduleTime;
        /**
         * 最早报到时间
         */
        private String reportStartTime;
        /**
         * 截至报到时间
         */
        private String reportEndTime;
        /**
         * 就医指引
         */
        private String guidelines;
        /**
         * 预约状态
         */
        private String appointStatus;
        /**
         * 挂号费
         */
        private String registFee;
        /**
         * 诊查费
         */
        private String clinicFee;
        /**
         * 科室编码
         */
        private String deptCode;
        /**
         * 科室名称
         */
        private String deptName;
        /**
         * 医生编码
         */
        private String doctorCode;
        /**
         * 医生姓名
         */
        private String doctor;
        /**
         * 号类
         */
        private String clinicType;
        /**
         * 撤销时间
         */
        private String appointCancelOperator;
        /**
         * 预约途径
         */
        private String appointSource;

        public QueryAppointDetailsInnerMessage(Element element) {

            this.clinicVisitDate = element.attributeValue("CLINIC_VISIT_DATE");
            this.clinicLabel = element.attributeValue("CLINIC_LABEL");
            this.timeDesc = element.attributeValue("TIME_DESC");
            this.hourStart = element.attributeValue("HOUR_START");
            this.hourEnd = element.attributeValue("HOUR_END");
            this.registOrder = element.attributeValue("REGIST_ORDER");
            this.apptedId = element.attributeValue("APPTED_ID");
            this.patientName = element.attributeValue("PATIENT_NAME");
            this.sex = element.attributeValue("SEX");
            this.chargeType = element.attributeValue("CHARGE_TYPE");
            this.dateOfBirth = element.attributeValue("DATE_OF_BIRTH");
            this.insuranceNo = element.attributeValue("INSURANCE_NO");
            this.idNo = element.attributeValue("ID_NO");
            this.phoneNumber = element.attributeValue("PHONE_NUMBER");
            this.appointDate = element.attributeValue("APPOINT_DATE");
            this.appointScheduleTime = element.attributeValue("APPOINT_SCHEDULE_TIME");
            this.reportStartTime = element.attributeValue("REPORT_START_TIME");
            this.reportEndTime = element.attributeValue("REPORT_END_TIME");
            this.guidelines = element.attributeValue("GUIDELINES");
            this.appointStatus = element.attributeValue("APPOINT_STATUS");
            this.registFee = element.attributeValue("REGIST_FEE");
            this.clinicFee = element.attributeValue("CLINIC_FEE");
            this.deptCode = element.attributeValue("DEPT_CODE");
            this.deptName = element.attributeValue("DEPT_NAME");
            this.doctorCode = element.attributeValue("DOCTOR_CODE");
            this.doctor = element.attributeValue("DOCTOR");
            this.clinicType = element.attributeValue("CLINIC_TYPE");
            this.appointCancelOperator = element.attributeValue("APPOINT_CANCEL_OPERATOR");
            this.appointSource = element.attributeValue("APPOINT_SOURCE");
        }

        public String getAppointCancelOperator() {
            return appointCancelOperator;
        }

        public void setAppointCancelOperator(String appointCancelOperator) {
            this.appointCancelOperator = appointCancelOperator;
        }

        public String getAppointSource() {
            return appointSource;
        }

        public void setAppointSource(String appointSource) {
            this.appointSource = appointSource;
        }

        public String getAppointStatus() {
            return appointStatus;
        }

        public void setAppointStatus(String appointStatus) {
            this.appointStatus = appointStatus;
        }

        public String getRegistFee() {
            return registFee;
        }

        public void setRegistFee(String registFee) {
            this.registFee = registFee;
        }

        public String getClinicFee() {
            return clinicFee;
        }

        public void setClinicFee(String clinicFee) {
            this.clinicFee = clinicFee;
        }

        public String getDeptCode() {
            return deptCode;
        }

        public void setDeptCode(String deptCode) {
            this.deptCode = deptCode;
        }

        public String getDeptName() {
            return deptName;
        }

        public void setDeptName(String deptName) {
            this.deptName = deptName;
        }

        public String getDoctorCode() {
            return doctorCode;
        }

        public void setDoctorCode(String doctorCode) {
            this.doctorCode = doctorCode;
        }

        public String getDoctor() {
            return doctor;
        }

        public void setDoctor(String doctor) {
            this.doctor = doctor;
        }

        public String getClinicVisitDate() {
            return clinicVisitDate;
        }

        public void setClinicVisitDate(String clinicVisitDate) {
            this.clinicVisitDate = clinicVisitDate;
        }

        public String getClinicLabel() {
            return clinicLabel;
        }

        public void setClinicLabel(String clinicLabel) {
            this.clinicLabel = clinicLabel;
        }

        public String getTimeDesc() {
            return timeDesc;
        }

        public void setTimeDesc(String timeDesc) {
            this.timeDesc = timeDesc;
        }

        public String getHourStart() {
            return hourStart;
        }

        public void setHourStart(String hourStart) {
            this.hourStart = hourStart;
        }

        public String getHourEnd() {
            return hourEnd;
        }

        public void setHourEnd(String hourEnd) {
            this.hourEnd = hourEnd;
        }

        public String getRegistOrder() {
            return registOrder;
        }

        public void setRegistOrder(String registOrder) {
            this.registOrder = registOrder;
        }

        public String getApptedId() {
            return apptedId;
        }

        public void setApptedId(String apptedId) {
            this.apptedId = apptedId;
        }

        public String getPatientName() {
            return patientName;
        }

        public void setPatientName(String patientName) {
            this.patientName = patientName;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public String getChargeType() {
            return chargeType;
        }

        public void setChargeType(String chargeType) {
            this.chargeType = chargeType;
        }

        public String getDateOfBirth() {
            return dateOfBirth;
        }

        public void setDateOfBirth(String dateOfBirth) {
            this.dateOfBirth = dateOfBirth;
        }

        public String getInsuranceNo() {
            return insuranceNo;
        }

        public void setInsuranceNo(String insuranceNo) {
            this.insuranceNo = insuranceNo;
        }

        public String getIdNo() {
            return idNo;
        }

        public void setIdNo(String idNo) {
            this.idNo = idNo;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }


        public String getAppointDate() {
            return appointDate;
        }

        public void setAppointDate(String appointDate) {
            this.appointDate = appointDate;
        }

        public String getAppointScheduleTime() {
            return appointScheduleTime;
        }

        public void setAppointScheduleTime(String appointScheduleTime) {
            this.appointScheduleTime = appointScheduleTime;
        }

        public String getReportStartTime() {
            return reportStartTime;
        }

        public void setReportStartTime(String reportStartTime) {
            this.reportStartTime = reportStartTime;
        }

        public String getReportEndTime() {
            return reportEndTime;
        }

        public void setReportEndTime(String reportEndTime) {
            this.reportEndTime = reportEndTime;
        }

        public String getGuidelines() {
            return guidelines;
        }

        public void setGuidelines(String guidelines) {
            this.guidelines = guidelines;
        }


        public String getClinicType() {
            return clinicType;
        }

        public void setClinicType(String clinicType) {
            this.clinicType = clinicType;
        }
    }
}

